package com.jt.dify.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jt.dify.common.R;
import com.jt.dify.model.ChatRecord;
import com.jt.dify.model.ChatRecordInfo;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.jt.dify.model.table.ChatRecordInfoTableDef.CHAT_RECORD_INFO;
import static com.jt.dify.model.table.ChatRecordTableDef.CHAT_RECORD;

/**
 * @author by HaiTao.Wang on 2025/3/3.
 */

@Mapping("chat")
@Controller
public class ChatController {

    /**
     * 开启新对话
     */
    @Mapping("save")
    public R save() {

        Long id = new ChatRecord().setContent("新对话").saveOpt().orElseThrow(RuntimeException::new).getId();

        return R.data(id);
    }

    /**
     * 获取对话列表
     */
    @Mapping("list")
    public R list() {

        List<ChatRecord> records = new ChatRecord().orderBy(CHAT_RECORD.UPDATE_TIME.desc()).list();

        //筛选今天的数据
        List<ChatRecord> today = records.stream()
                .filter(chatRecord -> DateUtil.isSameDay(chatRecord.getUpdateTime(), new Date()))
                .collect(Collectors.toList());

        //筛选7天内的数据
        List<ChatRecord> day7 = records.stream()
                .filter(chatRecord -> DateUtil.betweenDay(chatRecord.getUpdateTime(), new Date(), false) <= 7)
                .collect(Collectors.toList());

        //7天内移除本日的数据
        day7.removeAll(today);

        //筛选30天内的数据
        List<ChatRecord> day30 = records.stream()
                .filter(chatRecord -> DateUtil.betweenDay(chatRecord.getUpdateTime(), new Date(), false) <= 30)
                .collect(Collectors.toList());

        //3o天内移除本日和7天内的数据
        day30.removeAll(today);
        day30.removeAll(day7);

        JSONObject result = JSONUtil.createObj()
                .set("today", today)
                .set("day7", day7)
                .set("day30", day30);

        return R.data(result);
    }

    /**
     * 查询对话记录
     *
     * @param recordId 记录id
     * @return
     */
    @Mapping("find")
    public R save(String recordId) {

        if (StrUtil.isEmpty(recordId)) {
            return R.error("参数错误");
        }

        List<ChatRecordInfo> list = new ChatRecordInfo()
                .where(CHAT_RECORD_INFO.RECORD_ID.eq(recordId))
                .orderBy(CHAT_RECORD_INFO.CREATE_TIME.asc())
                .list();

        return R.data(list);
    }

    /**
     * 查询最新的对话记录
     *
     * @return
     */
    @Mapping("findNew")
    public R findNew() {

        //查询最新的对话记录
        ChatRecord record = new ChatRecord().orderBy(CHAT_RECORD.UPDATE_TIME.desc()).one();
        if (record == null) {
            return R.data(CollUtil.newArrayList());
        }

        //判断是否是今日
        if (!DateUtil.isSameDay(record.getUpdateTime(), new Date())) {
            return R.data(CollUtil.newArrayList());
        }

        List<ChatRecordInfo> list = new ChatRecordInfo()
                .where(CHAT_RECORD_INFO.RECORD_ID.eq(record.getId()))
                .orderBy(CHAT_RECORD_INFO.CREATE_TIME.asc())
                .list();

        return R.data(list);

    }

    /**
     * 删除对话记录
     *
     * @param recordId
     * @return
     */
    @Mapping("remove")
    public R remove(Long recordId) {

        boolean remove = new ChatRecord().setId(recordId).removeById();

        return remove ? R.data("删除成功") : R.error("删除失败");

    }
}
